package spring.cloud.tasks.executor.name_space.executor.tasks_runtime_info.task_item_assign;

import lombok.Getter;
import lombok.Setter;
import spring.cloud.tasks.common.task_item_allot.OnlineExecutor;
import spring.cloud.tasks.executor.name_space.executor.task.task_listener.task_item_allot.TaskItemAllotContentService;
import spring.cloud.tasks.executor.name_space.executor.tasks_runtime_info.task_item_execute.TaskItemExecuteRuntimeContext;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 实时维护当前执行器任务和任务片信息。
 * 那么就可以主动或者被动通知任务调度启动或停止任务调度以及任务分片信息的添加或者删除。hi
 * <p>
 * 两种机制 1ZK监听机制 2启动线程定时去拉取。两者需要给定一个版本。在获取数据的时候，需要进行加锁保持同步。
 * 1ZK监听机制优于2启动线程定时去拉取。
 */
@Setter
@Getter
public class TaskIdToTaskInstanceIdToTaskItemIndexListMapMap {
    public String executorId;
    private TaskItemAllotContentService taskItemAllotContentService;
    private ConcurrentHashMap<String, ConcurrentHashMap<String, List<Integer>>> taskIdToTaskInstanceIdToItemIndexListMapMap = new ConcurrentHashMap<>();

    public TaskIdToTaskInstanceIdToTaskItemIndexListMapMap(String executorId, TaskItemAllotContentService taskItemAllotContentService) {
        this.executorId = executorId;
        this.taskItemAllotContentService = taskItemAllotContentService;
    }

    public ConcurrentHashMap<String, List<Integer>> getTaskInstanceIdToItemIndexListMap(String taskId) {
        if (!taskIdToTaskInstanceIdToItemIndexListMapMap.containsKey(taskId)) {
            return new ConcurrentHashMap<>();
        }
        return taskIdToTaskInstanceIdToItemIndexListMapMap.get(taskId);
    }

    public boolean isAssigned(TaskItemExecuteRuntimeContext taskItemExecuteRuntimeContext) {

        String taskId = taskItemExecuteRuntimeContext.getTaskId();
        String taskInstanceId = taskItemExecuteRuntimeContext.getTaskInstanceId();
        Integer taskItemIndex = taskItemExecuteRuntimeContext.getTaskItemIndex();
        if (!taskIdToTaskInstanceIdToItemIndexListMapMap.containsKey(taskId)) {
            return false;
        }
        ConcurrentHashMap<String, List<Integer>> taskInstanceIdToItemIndexListMap = taskIdToTaskInstanceIdToItemIndexListMapMap.get(taskId);
        if (!taskInstanceIdToItemIndexListMap.containsKey(taskInstanceId)) {
            return false;
        }
        List<Integer> taskItemIndexList = taskInstanceIdToItemIndexListMap.get(taskInstanceId);
        return taskItemIndexList.contains(taskItemIndex);

    }

    public void get() throws Exception {
        OnlineExecutor onlineExecutor = taskItemAllotContentService.getOnlineExecutor(executorId);
        if (onlineExecutor == null) {
            this.taskIdToTaskInstanceIdToItemIndexListMapMap = new ConcurrentHashMap<>();
        } else {
            //需要将this.taskIdToItemIndexListMap=和taskIdToItemIndexListMapz进行对比
            //增加和删除task 增加和删除task item
            //在这主动获取当前任务的执行频率。（前30分钟+后30分钟+如果30分钟内不存在那么至少获取到下次执行的时间频率）
            this.taskIdToTaskInstanceIdToItemIndexListMapMap = taskItemAllotContentService.getTaskIdToTaskInstanceIdToItemIndexListMapMap(onlineExecutor);
        }
    }
}
